<?php

namespace App\Model\Service;

use App\Model\CartModel;
use Trois\Lib\Help;

class CartService extends BaseService
{
    public function list(): array
    {
//        return [];
        // 初始条件
        $where = [
            'cart.group_id' => $this->loginUser['group_id'],
            'cart.biz_id' => $this->loginUser['biz_id'],
            'cart.status' => [99, '!=']
        ];

        // 事业部
        if ($this->postParams['biz_id']) {
            $where['cart.biz_id'] = $this->postParams['biz_id'];
        }

        // 品牌
        if ($this->postParams['brand']) {
            $postBrand = explode(",", $this->postParams['brand']);
            $where['cart.bra_id'] = count($postBrand) == 1 ? $postBrand[0] : [$postBrand, 'in'];
        }

        // 城市
        if ($this->postParams['city']) {
            $postCity = explode(",", $this->postParams['city']);
            $where['cart.dept_city_id'] = count($postCity) == 1 ? $postCity[0] : [$postCity, 'in'];
        }

        // 门店
        if ($this->postParams['shop']) {
            $postShop = explode(",", $this->postParams['shop']);
            $where['cart.dept_id'] = count($postShop) == 1 ? $postShop[0] : [$postShop, 'in'];
        }

        // 预约部门
        if ($this->postParams['add_dept_id']) {
            $postAddDeptId = explode(",", $this->postParams['add_dept_id']);
            $where['cart.add_dept_id'] = count($postAddDeptId) == 1 ? $postAddDeptId[0] : [$postAddDeptId, 'in'];
        }

        // 预约人
        if ($this->postParams['add_user_id']) {
            $postAddUserId = explode(",", $this->postParams['add_user_id']);
            $where['cart.add_user_id'] = count($postAddUserId) == 1 ? $postAddUserId[0] : [$postAddUserId, 'in'];
        }

        // 排店部门
        if ($this->postParams['order_date_id_dept']) {
            $postOrderDeptId = explode(",", $this->postParams['order_date_id_dept']);
            $where['cart.order_date_id_dept'] = count($postOrderDeptId) == 1 ? $postOrderDeptId[0] : [$postOrderDeptId, 'in'];
        }

        // 排店人
        if ($this->postParams['order_date_id_user']) {
            $postOrderUserId = explode(",", $this->postParams['order_date_id_user']);
            $where['cart.order_date_id_user'] = count($postOrderUserId) == 1 ? $postOrderUserId[0] : [$postOrderUserId, 'in'];
        }

        // 性别 (0为未知，这里需要用!=='')
        if ($this->postParams['sex'] !== '') {
            $where['customer.cus_sex'] = $this->postParams['sex'];
        }

        // 预约类型
        if ($this->postParams['cus_type']) {
            $where['cart.cus_type'] = $this->postParams['cus_type'];
        }

        // 预约状态
        if ($this->postParams['status']) {
            $where['cart.status'] = $this->postParams['status'];
        }


        if ($this->postParams['is_undetermined'] == 1) {
            // 待定预约
            $date_start = strtotime($this->postParams['date_start']);
            $date_end = strtotime($this->postParams['date_end']);
//            $date_end = strtotime('+1 day', strtotime($this->postParams['date_end']));

            // 更新时间
            $where['cart.appoint_date'] = 0;
            $where['customer.wechat_join_day'] = [[$date_start, $date_end], 'BETWEEN'];

        } else {
            // 明确认日期预约
            $date_start = date('Ymd', strtotime($this->postParams['date_start']));
            $date_end = date('Ymd', strtotime($this->postParams['date_end']));

            // 预约时间
            if ($date_start == $date_end) {
                $where['cart.appoint_date'] = $date_start;
            } else {
                $where['cart.appoint_date'] = [[$date_start, $date_end], 'BETWEEN'];
            }

        }

        if ($this->postParams['keyword']) {
            switch ($this->postParams['keyword_type']) {
                case '3':
                    $where['customer.cus_mobile'] = $this->postParams['keyword'];
                    break;
                case '2':
                    $where['customer.cus_name'] = ['%' . $this->postParams['keyword'] . '%', 'like'];
                    break;
                case '1':
                default:
                    $where['customer.cus_id'] = $this->postParams['keyword'];
                    break;
            }
        }


        $cart = Help::modelInvoke(CartModel::class, function (CartModel $model) use ($where) {
            $list = $model
                ->field('cart.cart_id
                ,cart.dept_id
                ,cart.appoint_time
                ,cart.end_time
                ,cart.cus_type
                ,cart.status
                ,cart.projects
                ,cart.remarks
                ,cart.add_user_id
                ,cart.add_user_name
                ,cart.order_date_id_user
                ,order_user.user_name as order_user_name
                ,cart.cus_id
                ,customer.cus_name
                ,customer.cus_sex
                ,customer.cus_img
                ,shop.dept_name as shop_name
                ,brand.bra_name
                ,cart.dept_city_id
                ,area.area_name as city_name
                ,cart.appoint_follow
                ,appoint_follow.name as appoint_follow_name
                ,cart.appoint_follow_time
                ')
                ->alias('cart')
                ->join('by_common_customer as customer', 'customer.cus_id = cart.cus_id', 'left')
                ->join('by_crm_user as order_user', 'order_user.user_id = cart.order_date_id_user', 'left')
                ->join('by_crm_dept as shop', 'shop.dept_id = cart.dept_id', 'left')
                ->join('by_area as area', 'area.area_sn = cart.dept_city_id', 'left')
                ->join('by_crm_brand as brand', 'brand.bra_id = cart.bra_id', 'left')
                ->join('by_crm_appoint_follow as appoint_follow', 'appoint_follow.id = cart.appoint_follow', 'left')
                ->limit($this->postParams['limit'] * ($this->postParams['page'] - 1), $this->postParams['limit'])
                ->order('cart.cart_id', 'desc')
                ->withTotalCount()
                ->all($where)
                ->toArray(false, false);

            $total = $model->lastQueryResult()->getTotalCount();

            return [
                'total' => $total,                                                  // 总数据数
                'list' => $list,                                                    // 当前数据
                'pages' => ceil($total / $this->postParams['limit']),              // 总页数
                'page' => $this->postParams['page'],                                    // 当前所在页
                'limit' => $this->postParams['limit'],                                  // 每页数据数
            ];


        }, false);

//        dd(Help::getLastSql());

        return $cart ?: [
            'total' => 0,
            'list' => [],
            'pages' => 1,
            'page' => $this->postParams['page'],
            'limit' => $this->postParams['limit'],
        ];
    }
}